﻿using ProjectScheduling.daos;
using ProjectScheduling.models;

namespace ProjectScheduling
{
    public partial class AddStaffForm : Form
    {
        public static AddStaffForm b = new();
        public Action? action;
        public void FindById(int id)
        {
            InitData(StaffDao.Base.Find(id));
        }
        private int Id { get; set; }
        public void InitData(Staff? staff)
        {
            staff = staff ?? new();
            Id = staff.Id;
            txt_name.Text = staff.TrueName;
            txt_age.Text = staff.Age;
            txt_phone.Text = staff.Phone;
            r_0.Checked = staff.Sex == "女";
            r_1.Checked = staff.Sex == "男";
        }

        public AddStaffForm()
        {
            InitializeComponent();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            var obj = new Staff
            {
                Id = Id,
                TrueName = txt_name.Text.Trim(),
                Age = txt_age.Text.Trim(),
                Phone = txt_phone.Text.Trim(),
                Sex = r_0.Checked ? "女" : "男"
            };
            if (string.IsNullOrEmpty(obj.TrueName))
            {
                MessageBox.Show("姓名不能为空");
                return;
            }
            if (obj.Id == 0)
            {
                obj.CreateTime = DateTime.Now;
                StaffDao.Base.Add(obj);
            }
            else
            {
                StaffDao.Base.Update(obj, it => new { it.TrueName, it.Sex, it.Age, it.Phone });
            }
            MessageBox.Show("保存成功");
            action?.Invoke();
            Close();
        }
    }
}
